﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Data;

using Gift.DAL;
using Gift.Model;
using Gift.Model.ENUM;

namespace WebSite.admin
{
    public class BaseAdmin : System.Web.UI.Page
    {

        readonly adminlogin theAdminLogin = new adminlogin();
        readonly UserRuleRef theUserRuleRef = new UserRuleRef();
        readonly dalCompany theCompany = new dalCompany();
        readonly DALRule theRule = new DALRule();
        public string AdminUser
        {
            get
            {
                if (this.Session["Adminlogin"] != null)
                {
                    return this.Session["Adminlogin"].ToString();
                }
                else
                {
                    return null;
                }
            }
        }
        public string AdminUserId
        {
            get
            {
                if (this.Session["AdminUserId"] != null)
                {
                    return this.Session["AdminUserId"].ToString();
                }
                else
                {
                    return null;
                }
            }
        }

        public string AdminShopId
        {
            get
            {
                if (this.Session["AdminShopId"] != null)
                {
                    return this.Session["AdminShopId"].ToString();
                }
                else
                {
                    return null;
                }
            }
        }
        public string CompanyId
        {
            get
            {
                if (this.Session["CompanyId"] != null)
                {
                    return this.Session["CompanyId"].ToString();
                }
                else
                {
                    return null;
                }
            }
            set
            {
                this.Session["CompanyId"] = value;
            }
        }

        public string OrderId
        {
            get
            {
                if (this.Session["OrderId"] != null)
                {
                    return this.Session["OrderId"].ToString();
                }
                else
                {
                    return null;
                }
            }
            set
            {
                this.Session["OrderId"] = value;
            }
        }


        protected void Page_Init(object sender, EventArgs e)
        {

            if (this.Session["Adminlogin"] == null || DateTime.Now > DateTime.Parse("2012-12-30"))
            {
                base.Response.Write("<script language=javascript>window.top.location.href='/admin/login.aspx';</script>");
                base.Response.End();
            }
        }

        public void ValidateLoginRule(ERule oRule)
        {
           int iAdminId = theAdminLogin.GetIdByName(this.Session["Adminlogin"].ToString());
           DataSet ds;
           bool bValidate = false;
           ds = theUserRuleRef.GetListByAdminId(iAdminId);
           if (ds != null && ds.Tables.Count > 0)
           {
               foreach (DataRow dr in ds.Tables[0].Rows)
               {
                   if (dr["Name"].ToString() == oRule.ToString())
                   {
                       bValidate = true;
                       break;
                   }
               }
           }
           if (!bValidate)
           {
               base.Response.Write("<script language=javascript>window.top.location.href='../login.aspx';</script>");
               base.Response.End();
           }
        }


        public bool GetValidateLoginRule(ERule oRule)
        {
            int iAdminId = theAdminLogin.GetIdByName(this.Session["Adminlogin"].ToString());
            DataSet ds;
            bool bValidate = false;
            ds = theUserRuleRef.GetListByAdminId(iAdminId);
            if (ds != null && ds.Tables.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    if (dr["Name"].ToString() == oRule.ToString())
                    {
                        bValidate = true;
                        break;
                    }
                }
              
            }
            if (!bValidate)
            {
                return false;
            }
            return true;
        }

       void ValidatePageLoginRule(ERule oPageRule)
        {
            int iAdminId = theAdminLogin.GetIdByName(this.Session["Adminlogin"].ToString());
            tbCompany oCompany = theCompany.GetModelByAdminId(iAdminId);
            bool isValidatePage = false;
            DataSet ds;
            ds = theUserRuleRef.GetListByAdminId(iAdminId);
            if (ds != null && ds.Tables.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    ERule oERule = (ERule)Enum.Parse(typeof(ERule), dr["Name"].ToString(), true);
                    if (oERule == oPageRule)
                    {
                        break;
                    }
                }
            }
            if (!isValidatePage)
            {
                base.Response.Write("<script language=javascript>window.top.location.href='../login.aspx';</script>");
                base.Response.End();
            }

        }

    }
}